<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header :content="content" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form ref="carFormRef" label-width="100px" :model="carForm" :rules="carFormRules">
            <el-form-item label="车主姓名" prop="personName">
              <el-input v-if="!this.$route.params.type" v-model="carForm.personName" />
              <span v-else>{{ carForm.personName }}</span>
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-if="!this.$route.params.type" v-model="carForm.phoneNumber" />
              <span v-else>{{ carForm.phoneNumber }}</span>
              <!-- <template v-else #default="{ row }">
                {{ row }}
              </template> -->
            </el-form-item>
            <el-form-item label="车辆号码" prop="carNumber">
              <el-input v-if="!this.$route.params.type" v-model="carForm.carNumber" />
              <span v-else>{{ carForm.carNumber }}</span>
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input v-if="!this.$route.params.type" v-model="carForm.carBrand" />
              <span v-else>{{ carForm.carBrand }}</span>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">{{ !$route.params.type ? '最新一次月卡缴费信息' : '月卡缴费信息' }}</div>
        <div class="form">
          <el-form
            v-if="!this.$route.params.type"
            ref="payFormRef"
            label-width="100px"
            :model="payForm"
            :rules="payFormRules"
          >
            <el-form-item label="有效日期" prop="payTime">
              <el-date-picker
                v-model="payForm.payTime"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="getTime"
              />
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="payForm.paymentAmount" />
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="payForm.paymentMethod">
                <el-option v-for="item in paymentMethod" :key="item.value" :value="item.value" :label="item.value" />
              </el-select>
            </el-form-item>
          </el-form>
          <el-table v-else :data="tableData" highlight-current-row style="width: 100%">
            <el-table-column type="index" width="100" label="序号" />
            <el-table-column label="有效时间" width="250">
              {{ payForm.cardStartDate }} 至 {{ payForm.cardEndDate }}
            </el-table-column>
            <el-table-column property="paymentAmount" label="支付金额" width="120" />
            <el-table-column property="paymentMethod" label="支付方式" />
            <el-table-column property="createTime" label="办理时间" />
            <el-table-column property="createUser" label="办理人" />
          </el-table>
        </div>
      </div>

    </main>
    <footer v-if="!this.$route.params.type" class="add-footer">
      <div class="btn-container" style="text-align:center;">
        <el-button @click="onReset">重置</el-button>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { createCardApi, detailCardApi, editCardApi } from '@/api/card'
import { validaeCarNumber } from '@/utils/validate'

export default {
  data() {
    return {
      carForm: {
        personName: '',
        phoneNumber: '',
        carNumber: '',
        carBrand: ''
      },
      payForm: {
        cardStartDate: '',
        cardEndDate: '',
        payTime: [],
        paymentAmount: null,
        paymentMethod: null
      },
      paymentMethod: [
        {
          id: 'Alipy',
          value: '支付宝'
        },
        {
          id: 'WeChat',
          value: '微信'
        },
        {
          id: 'Cash',
          value: '现金支付'
        }
      ],
      carFormRules: {
        personName: [
          { required: true, message: '请输入车主姓名!', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '请输入手机号!', trigger: 'blur' },
          { pattern: /^1[34578]\d{9}$/, message: '请输入正确格式手机号', trigger: 'blur' }
        ],
        carNumber: [
          {
            required: true, message: '请输入车辆号码', trigger: 'blur'
          },
          {
            validator: validaeCarNumber, trigger: 'blur'
          }
        ],
        carBrand: [
          { required: true, message: '请输入车辆品牌', trigger: 'blur' }
        ]
      },
      payFormRules: {
        paymentAmount: [
          { required: true, message: '请输入支付金额!', trigger: 'blur' }
        ],
        paymentMethod: [
          { required: true, message: '请选择支付方式!', trigger: ['change', 'blur'] }
        ],
        payTime: [
          { required: true, message: '请选择支付时间', trigger: 'blur' }
        ]
      },
      tableData: []
    }
  },
  computed: {
    content() {
      if (this.$route.params.id) {
        if (this.$route.params.type) {
          return '查看详情'
        } else {
          return '编辑月卡'
        }
      }
      return '添加月卡'
    }
  },
  created() {
    this.$route.params.id && this.getCardInfo()
  },
  methods: {
    getTime() {
      // this.payForm.cardStartDate = this.payForm.payTime[0]
      // this.payForm.cardEndDate = this.payForm.payTime[1]
      this.$set(this.payForm, 'cardStartDate', this.payForm.payTime[0])
      this.$set(this.payForm, 'cardEndDate', this.payForm.payTime[1])
    },

    onReset() {
      this.$refs.carFormRef.resetFields()
      this.$refs.payFormRef.resetFields()
    },

    async onSubmit() {
      // 1、整体校验
      await this.$refs.carFormRef.validate()
      await this.$refs.payFormRef.validate()
      // 2、合并参数对象
      Object.assign(this.carForm, this.payForm)
      // console.log(this.carForm)
      // 3、删除无用参数
      // delete this.carForm.payTime
      // console.log(this.carForm.personName)

      if (this.$route.params.id) {
        const { personName, phoneNumber, carNumber, carBrand, cardStartDate, cardEndDate, paymentAmount, paymentMethod, carInfoId, rechargeId } = this.carForm
        await editCardApi({ personName, phoneNumber, carNumber, carBrand, cardStartDate, cardEndDate, paymentAmount, paymentMethod, carInfoId, rechargeId })
        this.$message.success('修改成功~')
      } else {
        const { personName, phoneNumber, carNumber, carBrand, cardStartDate, cardEndDate, paymentAmount, paymentMethod } = this.carForm
        // 4、发送请求
        await createCardApi({ personName, phoneNumber, carNumber, carBrand, cardStartDate, cardEndDate, paymentAmount, paymentMethod })
        this.$message.success('添加成功~')
      }
      // 5、添加成功返回
      this.$router.back()
    },

    async getCardInfo() {
      if (this.$route.params.id) {
        const res = await detailCardApi(this.$route.params.id)
        this.carForm = res
        this.payForm = res.rechargeList[0]
        //  cardStartDate: '',
        // cardEndDate: '',
        //   payTime: [],
        this.$set(this.payForm, 'payTime', [res.rechargeList[0].cardStartDate, res.rechargeList[0].cardEndDate])
        this.tableData.push(this.payForm)
        console.log('zheshigesha' + JSON.stringify(this.tableData))

        // this.payForm.payTime.push(res.rechargeList[0].cardStartDate)
        // this.payForm.payTime.push(res.rechargeList[0].cardEndDate)
        // this.$set(this.payForm.payTime, 0, res.rechargeList[0].cardStartDate)
        // this.$set(this.payForm.payTime, 1, )
      } else {
        await createCardApi()
      }
    }
  }
}
</script>

<style scoped lang="scss">
.add-card {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }

    .preview {
      img {
        width: 100px;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
